<template>
  <yt-page title="值班管理" :rightData="orgs.length ? rightData : []">
    <yt-group :group="false" :searchApi="searchApi" ref="group">
      <yt-card :edge="false" slot-scope="item" :title="item.orgId ? item.orgId.attributes.organization_name : ''">
        <yt-cell label="值班人" :value="item.duty_person" border="none"></yt-cell>
        <yt-cell label="值班地点" :value="item.duty_place" border="none"></yt-cell>
        <yt-cell label="联系电话" border="none">
          <yt-cell-call :tel="item.duty_phone || ''"></yt-cell-call>
        </yt-cell>
        <yt-cell label="开始时间" :value="item.start_time" border="none"></yt-cell>
        <yt-cell label="结束时间" :value="item.end_time" border="none"></yt-cell>
        <yt-textarea title="值班内容" :value="item.duty_content" border="none"></yt-textarea>
      </yt-card>
    </yt-group>

    <yt-popup pos="right" v-model="showSearch" :mask="true" slot="alert">
      <yt-page class="page" :flex="true" :leftData="[]" title="组织机构">
        <yt-form style="background-color: #fff">
          <yt-radioList class="list" :list="orgs" label="Name" val="id" v-model="selected" :inline="false" />
          <yt-btn-group slot="btn" edge>
            <yt-btn theme="white" @click="onReset()">重置</yt-btn>
            <yt-btn @click="onSearch(selected)">查询</yt-btn>
          </yt-btn-group>
        </yt-form>
      </yt-page>
    </yt-popup>
  </yt-page>
</template>

<script>
import { JudgeRole, GetStore, Store } from '@/store'
import { commonApi } from '@/common/api/commonApi.js'
import _ from 'lodash'

export default {
  data() {
    return {
      rightData: [
        {
          font: 'yt-icon yt-icon-search',
          click: () => {
            this.showSearch = !this.showSearch
          }
        }
      ],
      condition: null,
      searchApi: async ({ limit, skip }) => {
        let condition = {}
        if (this.orgs.length) {
          // 管理员权限登录
          condition = this.condition ? this.condition : {}
        } else {
          // 企业登录
          let enterpriseId = JudgeRole()
            .enterprise(() => {
              return (GetStore(Store.enterprise) || {}).id
            })
            .government(() => {
              return (GetStore(Store.enterprise) || {}).id
            })
            .default()
          condition = { ...this.condition, equalTo: { orgId: enterpriseId } }
        }
        let items = await commonApi.search('publicService_duty', { ...condition, include: ['orgId'], limit, skip })
        return items
      },
      showSearch: false,
      selected: '',
      orgs: []
    }
  },
  async activated() {
    this.orgs = await this.fetchOrgs()
  },
  methods: {
    async fetchOrgs() {
      let role = JudgeRole().administrator('admin').default()
      if (role === 'admin') {
        let orgs = await commonApi.search('sys_organizations', {
          limit: 1000,
          descending: 'Nature',
          or: [{ doesNotExist: 'parentCode' }, { equalTo: { parentCode: '' } }]
        })
        return orgs
      }
      return []
    },
    onSearch(selected) {
      this.condition = selected ? { equalTo: { orgId: selected } } : null
      this.$refs.group.startPull()
      this.showSearch = false
    },
    onReset() {
      this.selected = ''
      this.condition = null
      this.$refs.group.startPull()
      this.showSearch = false
    }
  }
}
</script>

<style lang="stylus" scoped>
>>>.yt-cell-label {
  padding: 0px;
}
</style>
